package org.forwhat.dao.Impl;

import org.forwhat.bean.WhatComment;
import org.forwhat.bean.WhatCommentDTO;
import org.forwhat.dao.WhatCommentDao;
import org.forwhat.framework.annotation.Data;
import org.forwhat.framework.annotation.InsertBean;
import org.forwhat.util.JDBCUtilities;

import java.sql.Connection;
import java.util.List;

/**
 * @projectName: ForWhat
 * @package: org.forwhat.dao.Impl
 * @className: WhatCommentDaoImpl
 * @author: LGJ
 * @description: TODO
 * @date: 2023/11/30 14:57
 * @version: 1.0
 */
//@Data("WhatCommentDaoImpl")
public class WhatCommentDaoImpl  implements WhatCommentDao {

    @InsertBean("BaseDaoImpl")
    private BaseDaoImpl baseDaoImpl;

    public BaseDaoImpl getBaseDaoImpl() {
        return baseDaoImpl;
    }

    public void setBaseDaoImpl(BaseDaoImpl baseDaoImpl) {
        this.baseDaoImpl = baseDaoImpl;
    }

    @Override
    public List<WhatCommentDTO> selectCommentByParent(String parent) {
        try {
            return baseDaoImpl.selectList("select * from what_comment where comment_parent = ?",WhatCommentDTO.class,parent);
        } catch (Exception e) {
            throw new RuntimeException(e);
        }
    }

    @Override
    public String selectUserIdByCommentId(String commentId) {
        try {
            return (String) baseDaoImpl.selectObject("select comment_user_id from what_comment where comment_id = ?",commentId);
        } catch (Exception e) {
            return null;
        }
    }

    @Override
    public int insertComment(WhatComment whatComment) {
        return baseDaoImpl.update("insert into what_comment(comment_parent, comment_content, comment_user_id, comment_create_time, comment_id) value (?,?,?,?,?)",
                whatComment.getCommentParent(),
                whatComment.getCommentContent(),
                whatComment.getCommentUserId(),
                whatComment.getCommentCreateTime(),
                whatComment.getCommentId());
    }

    @Override
    public String getLatestComment() {
        try {
            return (String) baseDaoImpl.selectObject("select comment_id from what_comment order by comment_create_time desc limit 1");
        } catch (Exception e) {
            return "comment1";
        }
    }
}
